<?php

namespace app\admin\controller\yunfood;

use addons\yunfood\library\Backend;
use addons\yunfood\model\Qrcode as QrcodeModel;
use addons\yunfood\model\Config;
use think\Exception;

/**
 * 二维码模块
 */
class Qrcode extends Backend
{
    protected $noNeedRight = ['preview'];

    /**
     * @var QrcodeModel
     */
    protected $model;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new QrcodeModel();
    }

    /**
     * 预览
     */
    public function preview()
    {
        $params = $this->request->get();
        $scene = $this->request->get('scene');
        $env = $this->request->get('env', 'release');
        $page = QrcodeModel::PAGE_HOME;
        if ($scene === 'shop') {
            $page = QrcodeModel::PAGE_SHOP_HOME;
        } elseif ($scene === 'desk') {
            $page = QrcodeModel::PAGE_FOOD_HOME;
        }
        unset($params['scene'], $params['env'], $params['dialog']);

        // 小程序版本
        $this->view->assign('env', $env);
        $this->view->assign('envList', $this->model::getEnvList());
        $this->assignconfig('envList', $this->model::getEnvList());

        // 小程序二维码
        try {
            if (! Config::getGroupAttribute(Config::GROUP_BASE, 'app_id')) {
                throw new Exception('小程序AppId未配置');
            }
            if (! Config::getGroupAttribute(Config::GROUP_BASE, 'app_secret')) {
                throw new Exception('小程序AppSecret未配置');
            }
            $miniQrcode = $this->model::createMiniQrcode($page, $params ? http_build_query($params) : '', false, $env);
            $this->view->assign('miniQrcode', $miniQrcode);
        } catch (Exception $e) {
            if (strpos($e->getMessage(), 'invalid page') !== false) {
                $this->view->assign('miniTip', '小程序新版本还未发布，可以在体验版或开发版上预览');
            } else {
                $this->view->assign('miniTip', $e->getMessage());
            }
        }

        // H5二维码
        try {
            if (! $domain = Config::getH5Domain()) {
                throw new Exception('H5域名未配置');
            }
            $content = trim($domain, '/') . '/#/' . $page;
            if ($params) {
                if ($scene) {
                    $content .= '?scene=' . urlencode(http_build_query($params));
                } else {
                    $content .= '?' . http_build_query($params);
                }
            }
            $normalQrcode = $this->model::createNormalQrcode($content, false);
            $this->view->assign('normalQrcode', $normalQrcode);
        } catch (Exception $e) {
            $this->view->assign('normalTip', $e->getMessage());
        }

        return $this->view->fetch();
    }

    public function index()
    {
        return;
    }

    public function add()
    {
        return;
    }

    public function edit($ids = null)
    {
        return;
    }

    public function del($ids = null)
    {
        return;
    }

    public function multi($ids = null)
    {
        return;
    }
}
